﻿using System.Web.Mvc;
using FriendStar.Code;
using FriendStar.Application.BaseData;
using System.Collections.Generic;

namespace FriendStar.Web.Areas.Mould.Controllers
{
    public class MouldArchiveController : ControllerBase
    {
        private string UseUnit = Configs.GetValue("UseUnit");

        MouldArchiveApp app = new MouldArchiveApp();


        public ActionResult MouldArchiveManagement()
        {
            ViewData["useUnit"]  = Configs.GetValue("UseUnit");

            return View();
        }

        public ActionResult MouldArchiveAdd(string id)
        {
            if (null != id)
            {
                ViewData["jsonData"] = app.GetMouldArchivie(id).ToJson();
            }
            else
            {
                ViewData["jsonData"] = "null";
            }

            return View();
        }
        public ActionResult MouldArchiveUnits()
        {
            return View();
        }
        public ActionResult MouldArchivePartNumbers()
        {
            return View();
        }

        public ActionResult GetMouldArchivies(Pagination pagination, string MoldNO, string MoldName, string AssetsNO, string MoldUnit, string MoldWorkshop, string WorkpieceNo, string ManuUnit)
        {
            string useUnit = Configs.GetValue("UseUnit");

            var data = new
            {
                rows = app.GetMouldArchivies(pagination, MoldNO, MoldName, AssetsNO, MoldUnit, MoldWorkshop, WorkpieceNo, ManuUnit, useUnit),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        public ActionResult GetMouldArchivieUnits(Pagination pagination, string FProductNO)
        {
            string useUnit = Configs.GetValue("UseUnit");

            var data = new
            {
                rows = app.GetMouldArchivieUnits(pagination, FProductNO, useUnit),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };

            return Content(data.ToJson());
        }

        public ActionResult GetMouldArchiviePartNumbers(Pagination pagination, string FProductionNO)
        {
            string useUnit = Configs.GetValue("UseUnit");

            var data = new
            {
                rows = app.GetMouldArchiviePartNumbers(pagination, FProductionNO, useUnit),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };

            return Content(data.ToJson());
        }



        public ActionResult DeleteMouldArchive(string id)
        {
            var data = app.DeleteMouldArchive(id);

            return Content(data.ToJson());
        }
        public ActionResult ChangeMouldArchive(string type,string ids)
        {
            string curUser = OperatorProvider.Provider.GetCurrent().UserName;

            System.DateTime now = System.DateTime.Now;

            var data = app.ChangeMouldArchive(type,ids,curUser,now);

            return Content(data.ToJson());
        }


        public ActionResult GetModCategories(string FClassNO)
        {
            string useUnit = Configs.GetValue("UseUnit");

            var data = app.GetModCategories(useUnit, FClassNO);

            return Content(data.ToJson());
        }

        public ActionResult GetUseUnits()
        {
            string useUnit = Configs.GetValue("UseUnit");

            var data = app.GetUseUnits(useUnit);

            return Content(data.ToJson());
        }

        public ActionResult UpdateMouldArchive(string param)
        {
            string useUnit = Configs.GetValue("UseUnit");

            string curUser = OperatorProvider.Provider.GetCurrent().UserName;

            System.DateTime now = System.DateTime.Now;

            Dictionary<string, object> item = new Dictionary<string, object>();

            item = param.ToObject<Dictionary<string, object>>();

            object data = null;

            item.Add("UseUnit", useUnit);

            if (item["Moldid"].Equals(string.Empty))
            {
                item["Moldid"] = System.Guid.NewGuid();

                item.Add("InputUser", curUser);

                item.Add("InputDate", now.ToString("yyyy-MM-dd"));

                item.Add("InputTime", now.ToString("HH:mm:ss"));

                data = app.InsertMouldArchive(item);
            }
            else
            {
                item.Add("FUpdateBy", curUser);

                item.Add("FUpdateDate", now.ToString("yyyy-MM-dd"));

                item.Add("FUpdateTime", now.ToString("HH:mm:ss"));

                data = app.UpdateMouldArchive(item);
            }

            return Content(data.ToJson());
        }

    }
}
